<template>
  <div class="rightbox" v-cloak>
    <h1>排班列表</h1>
    <el-row style="width: 96%;margin:2%;min-width:800px;">
      <div :span="3" style="float: left;width:150px;">
        <el-select v-model="doctorindex" multiple placeholder="请选择医生">
          <el-option
            v-for="item in doctorData"
            :key="item.doctorindex"
            :label="item.name"
            :value="item.doctorindex">
          </el-option>
        </el-select>
      </div>
      <div style="float: left;width:150px;margin-left:20px;">
        <el-cascader
          placeholder="请选择科室"
          clearable
          expand-trigger="hover"
          :options="departData"
          :show-all-levels="false"
          v-model="departmentindex">
        </el-cascader>
      </div>
      <div :span="3" style="margin-left:20px;float: left;width:150px;">
        <el-date-picker
          style="width:150px;"
          v-model="starttime"
          type="date"
          placeholder="开始日期">
        </el-date-picker>
      </div>
      <div :span="3" style="margin-left:20px;float: left;width:150px;">
        <el-date-picker
          style="width:150px;"
          v-model="endtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="结束日期">
        </el-date-picker>
      </div>
      <div :span="1" style="margin-left:20px;float: left;width:200px;">
        <el-button type="primary" @click="Search">查询</el-button>
        <el-button type="primary" @click="Delete">删除</el-button>
      </div>
    </el-row>
    <el-table
      style="width: 96%;margin:2%;"
      :data="timeList"
      border>
      <el-table-column
        prop="doctorname"
        label="姓名"
      >
      </el-table-column>
      <el-table-column
        prop="departmentname"
        label="科室"
      >
      </el-table-column>
      <el-table-column
        prop="numbername"
        label="号别">
      </el-table-column>
      <el-table-column
        label="排班时间">
        <template slot-scope="scope">
          <span>{{scope.row.starttime.split(' ')[0]}} {{scope.row.weekModel.weekStr}} {{scope.row.weekModel.timeStr}}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import axios from 'axios'
import qs from 'qs'
export default {
  data () {
    return {
      doctorData: [],
      starttime: '',
      endtime: '',
      doctorindex: [],
      timeList: [],
      departData: [],
      departmentindex: []
    }
  },
  methods: {
    Search: function () {
      var that = this
      that.getList()
    },
    getList: function () {
      var that = this
      that.$pub.open()
      axios.post('/gp/Dutyday/Index', qs.stringify({
        starttime: that.starttime,
        endtime: that.endtime,
        doctorindex: that.doctorindex,
        Departmentindex: that.departmentindex[1]
      })
      )
        .then((res) => {
          that.$pub.close()
          if (res.data.code === 0) {
            that.timeList = res.data.data
          } else {
            that.$pub.failcode(that, res.data.code, res.data.data)
          }
        })
        .catch(function () {
          that.$pub.catchbox(that)
        })
    },
    Delete: function () {
      var that = this
      that.$pub.open()
      axios.post('/gp/Dutyday/Delete', qs.stringify({
        starttime: that.starttime,
        endtime: that.endtime,
        doctorindex: that.doctorindex
      })
      )
        .then((res) => {
          that.$pub.close()
          if (res.data.code === 0) {
            that.$pub.messageTwo(res.data.data)
            that.getList()
          } else {
            that.$pub.failcode(that, res.data.code, res.data.data)
          }
        })
        .catch(function () {
          that.$pub.catchbox(that)
        })
    },
    getData: function () {
      var that = this
      that.$pub.open()
      axios.post('/gp/Doctor/Index')
        .then((res) => {
          that.$pub.close()
          if (res.data.code === 0) {
            that.doctorData = res.data.data
          } else {
            that.$pub.failcode(that, res.data.code, res.data.data)
          }
        })
        .catch(function () {
          that.$pub.catchbox(that)
        })
      axios({
        method: 'post',
        url: '/gp/Department/Index'
      })
        .then((res) => {
          that.$pub.close()
          if (res.data.code === 0) {
            var departData = []
            for (var i = 0; i < res.data.data.length; i++) {
              departData.push({value: res.data.data[i].departmentindex, label: res.data.data[i].name, children: []})
              for (var j = 0; j < res.data.data[i].subset.length; j++) {
                departData[i].children.push({value: res.data.data[i].subset[j].departmentindex, label: res.data.data[i].subset[j].name})
              }
            }
            that.departData = departData
          } else {
            that.$pub.failcode(that, res.data.code, res.data.data)
          }
        })
        .catch(function () {
          that.$pub.catchbox(that)
        })
    }
  },
  mounted () {
    this.getData()
  }
}
</script>
